<template>
  <el-dialog class="batchModify" destroy-on-close center v-model="visible" title="离职继承" width="40%" @closed="onReset">
    <el-form :model="dialogDatas" ref="formRef" class="px12">
      <el-form-item label="归属组织">
        <el-input v-model="dialogDatas.orgName" disabled />
      </el-form-item>
      <el-form-item label="现在客户转交">
        <el-select v-model="dialogDatas.extendId" placeholder="请选择转交人员">
          <el-option v-for="item in healthDoctorOptions" :label="item.name" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="adminUserResignRequestFun">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, nextTick, watch } from 'vue';
import type { FormInstance } from 'element-plus';
import type { IRoleItem } from '@/api/role/type';
// import { batchChangeOwner } from '@/api/checkPointEquipment';
import { adminUserResignRequest,getDoctorRequest } from '@/api/system/adminUser/index'; //api

const emits = defineEmits(['onConfirm']);

const visible = ref(false);
const formRef = ref<FormInstance>();
const dialogDatas = reactive({
  orgName: '',
  orgId: null,
  extendId: null,
  resignId: null

});
const init = async (data?: IRoleItem) => {
  visible.value = true;
  if (data) {
    // await nextTick();
    dialogDatas.orgName = data.rowData.orgName;
    dialogDatas.orgId = data.rowData.orgId;
    dialogDatas.resignId = data.rowData.id;
    getHealthDoctorOptions(data.rowData)
  }
};
// 客户转交下拉
let healthDoctorOptions = ref([])
const getHealthDoctorOptions = async (rowData: any) => {
  if (rowData) {
    const data: any = await getDoctorRequest({ orgId: rowData.orgId,reginId:rowData.id});
    healthDoctorOptions.value = data.data;
  } else {
    healthDoctorOptions.value = []
  }
};
// 离职继承
const adminUserResignRequestFun = async () => {
  if (!dialogDatas.extendId) {
    ElMessage.error('客户转交不能为空')
    return
  } else {
    let params = {
      resignId: dialogDatas.resignId,
      extendId: dialogDatas.extendId
    }
    const data: any = await adminUserResignRequest(params);
    if (data.code === 2000) {
      ElMessage.success('确认成功')
      visible.value = false;
      onReset()
    } else {
      ElMessage.error(data?.message)
    }
  }
}
const onReset = () => {
  if (!formRef.value) return;
  formRef.value.resetFields();
  dialogDatas.extendId = null;
};
defineExpose({ init });
</script>

<style lang="scss">
.batchModify {
  .el-form-item {
    display: block;
  }
  .el-form-item__content {
    width: 200px;
  }
}
</style>
